//
//  MainViewController.swift
//  MyLottery
//
//  Created by wujunjie on 2018/7/18.
//  Copyright © 2018年 wujunjie. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    ///懒加载属性
    lazy var composeBtn : UIButton = {
        let composeBtn = UIButton(image:  "tabbar_compose_icon_add", backgroundImage: "tabbar_compose_button", title: "", target: self, action: #selector(didClickComposeButton), event: .touchUpInside)
        return composeBtn
    }()
    
    
    /// MARK: 生命周期函数
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.addComposeButton()
        self.setTabbarLine()
        ///如果登录显示欢迎页或者新特性页
//        if UserInfoModel.userAccount.isLogin {
//            addNewFeatureViewOrWelconmeView()
//        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

//extensin 的三种用法的回顾
//扩展类的功能    实现协议和代理   把实现相同或相似功能的代码写到一个 extension 中
extension MainViewController {
    func setupUI(){
        //或者设置
//        self.tabBar.tintColor = UIColor.orange
        self.tabBar.barTintColor = UIColor(red: 0/255, green:188/255, blue:212/255, alpha:1)
        self.tabBar.isTranslucent = false
        self.addViewControllers()
    }
    ///添加子控制器
    func addViewControllers(){
        let homeVC = HomeViewController()
        addOneChildViewcontroller(viewController: homeVC, title: "首页", icon: "tabbar_home")

        let messageVC = MessageViewController()
        addOneChildViewcontroller(viewController: messageVC, title: "消息", icon: "tabbar_message_center")
        
        let composeVC = ComposeViewController()
        addOneChildViewcontroller(viewController: composeVC, title: " ", icon: " ")
        
        let  discoverVC = DiscoverViewController()
        addOneChildViewcontroller(viewController: discoverVC, title: "发现", icon: "tabbar_discover")
        
        let profileVC = ProfileViewController()
        addOneChildViewcontroller(viewController: profileVC, title: "我", icon: "tabbar_profile")
    }
    ///添加一个子控制器
    func addOneChildViewcontroller(viewController:UIViewController,title:String,icon:String){
        viewController.title = title
        viewController.tabBarItem.image = UIImage(named: icon)?.withRenderingMode(.alwaysOriginal)
        viewController.tabBarItem.selectedImage = UIImage(named: "\(icon)_selected")?.withRenderingMode(.alwaysOriginal)
        viewController.tabBarItem.setTitleTextAttributes([NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue):UIColor.gray,NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue):UIFont.systemFont(ofSize: 13)], for: .normal)
         viewController.tabBarItem.setTitleTextAttributes([NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue):UIColor.red,NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue):UIFont.systemFont(ofSize: 13)], for: .selected)
        let navVC = BaseNavViewController(rootViewController: viewController)
        addChildViewController(navVC)
    }
    ///添加发布按钮
    func addComposeButton(){
        composeBtn.center = CGPoint(x:UIScreen.main.bounds.size.width/2,y:tabBar.bounds.size.height/2)
        tabBar.addSubview(composeBtn)
    }
    ///添加 tabBar 的线
    func setTabbarLine(){
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        tabBar.shadowImage = UIImage.useColorCreateImage(color: UIColor.black, size: CGSize(width: 1, height: 1))
    }
}

extension MainViewController{
    ///点击了发布按钮
    @objc   func didClickComposeButton(){
        print("点击了我")
    }
}

extension MainViewController{
    private func addNewFeatureViewOrWelconmeView(){
        //如果有新特性
        if !Bundle.isNewVersion() {
            //显示新特性页
            let newFeature = NewFeatureView()
            view.addSubview(newFeature)
        } else {
            //显示欢迎页
            let welcome = WelcomeView()
            view.addSubview(welcome)
        }
    }
}

